//
//  XWHAQAQuestionTBCell.swift
//  XiaoweHearingAid
//
//  Created by edy on 2024/11/9.
//

import UIKit

class XWHAQAQuestionTBCell: XWHAQAReplyTBCell {
    
    lazy var imageBtns: [UIButton] = []
    
    var clickImageCallback: ((Int) -> Void)?
    
    var imageBtnHeight: CGFloat {
        return Self.imageHeight
    }
    var imageBtnWidth: CGFloat {
        return imageBtnHeight
    }

    override func addSubViews() {
        super.addSubViews()
        
        topLine.isHidden = true
        bottomLine.isHidden = true
        
        textContentView.layer.cornerRadius = 7
        textContentView.layer.backgroundColor = collectionBgColor.cgColor
        
        titleLb.textAlignment = .right
        subTitleLb.textAlignment = .right
        
        titleLb.textColor = majorTextColor
        
        textContentView.textLb.textAlignment = .left
        textContentView.textLb.numberOfLines = 0
        
        titleLb.text = "用户问题"
        subTitleLb.text = ""
        textContentView.textLb.text = ""
    }
    
    override func relayoutSubViews() {
        relayoutTitleSubTitleLb(isLeft: true)
        relayoutTopBottomLine()
    }
    
    func getImageBtns(imageUrlList: [String]) -> [UIButton] {
        var tBtnList: [UIButton] = []
        for (i, iText) in imageUrlList.enumerated() {
            let tBtn = UIButton()
            tBtn.layer.cornerRadius = 7
            tBtn.layer.masksToBounds = true
//            tBtn.layer.backgroundColor = UIColor(hex: 0xF7F7F7)!.cgColor
            tBtn.tag = i
            tBtn.kf.setImage(with: iText.url, for: .normal)
            tBtn.addTarget(self, action: #selector(clickImageBtn(sender:)), for: .touchUpInside)
            tBtnList.append(tBtn)
        }
        
        return tBtnList
    }
    
    @objc private func clickImageBtn(sender: UIButton) {
        clickImageCallback?(sender.tag)
    }
    
    func addImageBtns(imageUrlList: [String]) {
        imageBtns = getImageBtns(imageUrlList: imageUrlList)
        imageBtns.forEach({ contentView.addSubview($0) })
    }
    
    func relayoutImageBtns() {
        if frame.width <= 0 {
            return
        }
        
        if imageBtns.isEmpty {
            textContentView.snp.remakeConstraints { make in
                make.left.right.equalToSuperview().inset(Self.margin)
                make.top.equalTo(subTitleLb.snp.bottom).offset(4)
                make.bottom.equalToSuperview().inset(4)
            }
            return
        }

        let itemWidth = imageBtnWidth
        let itemHeight = imageBtnHeight
        
        var preBtn: UIButton?
        for iBtn in imageBtns {
            if let tPreBtn = preBtn {
                iBtn.snp.remakeConstraints { make in
                    make.width.height.top.equalTo(tPreBtn)
                    make.left.equalTo(tPreBtn.snp.right).offset(8)
                }
            } else {
                iBtn.snp.remakeConstraints { make in
                    make.width.equalTo(itemWidth)
                    make.height.equalTo(itemHeight)
                    make.left.equalToSuperview().inset(16)
//                    make.top.equalTo(subTitleLb.snp.bottom).offset(8)
                    make.bottom.equalToSuperview().inset(4)
                }
                
                textContentView.snp.remakeConstraints { make in
                    make.left.right.equalToSuperview().inset(Self.margin)
                    make.top.equalTo(subTitleLb.snp.bottom).offset(4)
                    make.bottom.equalTo(iBtn.snp.top).offset(-4)
                }
            }
            preBtn = iBtn
        }
    }
    
    func clearImageBtns() {
        imageBtns.forEach({ $0.removeFromSuperview() })
        imageBtns.removeAll()
    }
    
    func update(imageUrlList: [String]) {
        clearImageBtns()
        addImageBtns(imageUrlList: imageUrlList)
        relayoutImageBtns()
    }
    
    override func update(info: XWHAQAModel) {
        titleLb.text = "用户问题"
        subTitleLb.text = info.createdAt
        textContentView.textLb.numberOfLines = 0
        textContentView.textLb.text = info.content
        update(imageUrlList: info.extraImg)
    }

}
